<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.3 Increment/Decrement Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:02 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-3">4.3 Increment/Decrement Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.INCR1"></A><A NAME="CH04.INCR2"></A><A NAME="CH04.INCR3"></A><A NAME="CH04.INCR4"></A><A NAME="CH04.INCR5"></A>The <tt CLASS=literal>++</tt> operator is used to increment
the contents of a variable or an array element by one, while the
<tt CLASS=literal>- -</tt> operator is used
to decrement such a value by one.
The operand of <tt CLASS=literal>++</tt> or <tt CLASS=literal>- -</tt> must
evaluate to a variable or an array element; it cannot be an expression
that produces a pure value. For example, the following operations
succeed because the operand of the <tt CLASS=literal>++</tt> operator
produces a variable:

<DIV CLASS=screen>
<P>
<PRE>
int g = 0;
g++;
</PRE>
</DIV>

<P CLASS=para>
However, the following uses of <tt CLASS=literal>++</tt> generate error messages:

<DIV CLASS=screen>
<P>
<PRE>
final int h = 23;
h++;
5++;
</PRE>
</DIV>

<P CLASS=para>
The expression <tt CLASS=literal>h++</tt> produces an error because
<tt CLASS=literal>h</tt> is declared <tt CLASS=literal>final</tt>, which
means that its value cannot be changed. The expression <tt CLASS=literal>5++</tt>
generates an error message because 5 is a literal value, not a variable.

<P CLASS=para>
The increment and decrement operators can be used in both
postfix expressions (e.g., <tt CLASS=literal>i++</tt> or <tt CLASS=literal>i- -</tt>)
and in prefix expressions (e.g., <tt CLASS=literal>++i</tt> or <tt CLASS=literal>- -i</tt>).
Although both types of expression have the same side effect of incrementing
or decrementing a variable, they differ in the values that they
produce. A postfix expression produces a pure value that is the
value of the variable before it is incremented or decremented, while
a prefix expression produces a pure value that is the value of the
variable after it has been incremented or decremented. For example,
consider the following code fragment:

<DIV CLASS=screen>
<P>
<PRE>
int i = 3, j = 3;
System.out.println( "i++ produces " + i++);
System.out.println( "++j produces " + ++j);
</PRE>
</DIV>

<P CLASS=para>
The above code fragment produces
the following output:

<DIV CLASS=screen>
<P>
<PRE>
i++ produces 3
++j produces 4
</PRE>
</DIV>

<P CLASS=para>
After the code fragment has been evaluated, both <tt CLASS=literal>i</tt>
and <tt CLASS=literal>j</tt> have the value 4.

<P CLASS=para>
In essence,
what you need to remember is that a prefix expression performs its
increment or decrement before producing a value, while a postfix
expression performs its increment or decrement after producing a
value.

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-3.1">Postfix Increment/Decrement Operators</A></h3>

<P CLASS=para>
A postfix increment/decrement expression is a primary expression that
may be followed by either a <tt CLASS=literal>++</tt> or a <tt CLASS=literal>- -</tt>:


<p>
<img align=middle src="./figs/jlr0412.gif" alt="[Graphic: Figure from the text]" width=450 height=155 border=0>

<P CLASS=para>
The postfix increment and decrement operators are equal in precedence
and are effectively non-associative.

<P CLASS=para>
If a postfix expression
includes a <tt CLASS=literal>++</tt> or <tt CLASS=literal>- -</tt>, the primary
expression must produce a variable or an array element of an arithmetic
type. The postfix increment operator (<tt CLASS=literal>++</tt>) has
the side effect of incrementing the contents of the variable or
array element by one. The postfix decrement operator (<tt CLASS=literal>- -</tt>)
has the side effect of decrementing the contents of the variable
or array element by one.

<P CLASS=para>
The data type of the value
produced by a postfix increment/decrement operator is the same as
the data type of the variable or array element produced by the primary
expression. A postfix increment/decrement operator produces the
original pure value stored in the variable or array element before
it is incremented or decremented.

<P CLASS=para>
The following is an
example of using a postfix decrement operator:

<DIV CLASS=screen>
<P>
<PRE>
char j = '\u0100';
while (j-- &gt; 0)        // call doit for char values
    doit(j);           // '\u00ff' through '\u0000'
</PRE>
</DIV>

<P CLASS=para>
This
example works because Java treats <tt CLASS=literal>char</tt> as an
arithmetic data type.

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>;
<A HREF="ch04_01.htm#JLR2-CH-4-SECT-1">Primary Expressions</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-3.2">Prefix Increment/Decrement Operators</A></h3>

<P CLASS=para>
A prefix increment/decrement expression is a primary expression that
may be preceded by either a <tt CLASS=literal>++</tt> or a <tt CLASS=literal>- -</tt>:


<p>
<img align=middle src="./figs/jlr0413.gif" alt="[Graphic: Figure from the text]" width=425 height=123 border=0>

<P CLASS=para>
The prefix increment and decrement operators are equal in precedence
and are effectively non-associative.

<P CLASS=para>
If a prefix expression
includes a <tt CLASS=literal>++</tt> or <tt CLASS=literal>- -</tt>, the primary
expression must produce a variable or an array element of an arithmetic
type. The prefix increment operator (<tt CLASS=literal>++</tt>) has
the side effect of incrementing the contents of the variable or
array element by one. The prefix decrement operator (<tt CLASS=literal>- -</tt>)
has the side effect of decrementing the contents of the variable
or array element by one.

<P CLASS=para>
The data type of the value
produced by a prefix increment/decrement operator is the same as
the data type of the variable or array element produced by the primary
expression. A prefix increment/decrement operator produces the pure
value stored in the variable or array element after it has been
incremented or decremented.

<P CLASS=para>
Here's an example of using
a prefix increment operator:

<DIV CLASS=screen>
<P>
<PRE>
void foo(int a[]) {
    int j = -1;
    while (++j &lt; a.length)    // call doit for each element
        doit(a[j]);              // of a
    }
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>;
<A HREF="ch04_01.htm#JLR2-CH-4-SECT-1">Primary Expressions</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Allocation Expressions</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Unary Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
